package com.register.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBConnection {

	private static Connection connection = null;

	private DBConnection() {
		try {

			Class.forName("oracle.jdbc.driver.OracleDriver");

			System.out.println("DB Connection is ok.");

			String url = "jdbc:oracle:thin:@192.168.0.123:1521:orcl";
			String user = "forrest";
			String password = "1234";

			connection = DriverManager.getConnection(url, user, password);

			System.out.println("DriverManager is loaded.");
		} catch (ClassNotFoundException e) {
			System.err.println("DBConnectionMgr() is failed.");
			e.printStackTrace();
		} catch (SQLException e) {
			System.err.println("DBConnectionMgr() SQL is failed.");
			e.printStackTrace();
		}
	}

	public static Connection getConnection() {
		if (connection == null) new DBConnection();
		return connection;
	}

	public static void destroy(Connection conn, ResultSet rs, Statement st) {
		try {
			if (conn != null) conn.close();
			if (rs != null) rs.close();
			if (st != null) st.close();

			System.out.println("DB is closed now.");
		} catch (SQLException e) {
			System.err.println("DB closing is failed.");
			e.printStackTrace();
		}
	}
}
